﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using PharmacyBussinesEntities;
using PharmacyService;


namespace PharmacyWeb.Almacen
{
    public partial class frmProductoLote : System.Web.UI.Page
    {
      [Dependency]
        ILoteService LoteService { get; set; }

        [Dependency]
        public IProductoService ProductoService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            var accion = Request.QueryString["accion"];
            var idLote = Request.QueryString["id"];

            if (accion != null && accion == "nuevo")
            {
               // fvLotes.DefaulMode = FormView.Insert;
            }
            else if (accion != null && accion == "eliminar" && idLote != null)
            {
                var id = Int32.Parse(idLote.ToString());

                LoteService.RemoveLote(id);
                Response.Redirect("frmListProductoLote.aspx");
            }
        }

        public Lote GetLote([QueryString("identificador")] Int32? id)
        {
            Lote lote = null;
            if (id.HasValue)
                lote = LoteService.GetFromLoteById(id.Value);
            return lote;
        }

        public IEnumerable<Producto> GetProductos()
        {
            return ProductoService.GetAllFromProductos();
        }

        public void UpdateLote(Lote lote)
        {
            if (ModelState.IsValid)
            {
                LoteService.UpdateLote(lote);
                Response.Redirect("frmListProductoLote.aspx");
            }
        }

        public void InsertLote (Lote lote)
        {
            lote.Id = 0;

            if (ModelState.IsValid)
            {
                LoteService.AddLote(lote);
                Response.Redirect("frmListProductoLote.aspx");
            }
        }
        }
    }
